package com.forum.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.forum.common.utils.R;
import com.forum.community.entity.ForumAnnouncement;
import com.forum.community.entity.ForumCommunityAnnouncement;
import com.forum.community.service.ForumAnnouncementService;
import com.forum.community.service.ForumCommunityAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/community/forumAnnouncement")
public class ForumAnnouncementController {
    @Autowired
    private ForumAnnouncementService announcementService;

    @Autowired
    private ForumCommunityAnnouncementService communityAnnouncementService;

    //根据community_id列出公告列表
    @GetMapping("listAnnouncement/{communityId}")
    public R listAnnouncement(@PathVariable Long communityId){
        List<ForumAnnouncement> announcementList = announcementService.getAnnocement(communityId);
        if(announcementList.size()>0){
            return R.ok().put("forumAnnouncementList",announcementList);
        }
        return R.error();
    }

    //新增公告
    @PostMapping("addAnnouncement")
    public R addAnnocement(@RequestBody ForumAnnouncement forumAnnouncement){
        boolean i = announcementService.save(forumAnnouncement);
        //加入公告到公告社区表
        ForumCommunityAnnouncement communityAnnouncement = new ForumCommunityAnnouncement();
        communityAnnouncement.setAnnouncementId(forumAnnouncement.getId());
        communityAnnouncement.setCommunityId(forumAnnouncement.getCommunityId());
        communityAnnouncementService.save(communityAnnouncement);
        if(i==true){
            return  R.ok("增加公告成功！");
        }
        return R.error("增加公告失败！");
    }

    //删除公告
    @DeleteMapping("deleteAnnouncement/{announcementId}")
    public R deleteAnnouncement(@PathVariable Long announcementId){
        boolean i = announcementService.removeById(announcementId);
        //删除公告社区表中的数据
        QueryWrapper<ForumCommunityAnnouncement> query = new QueryWrapper<>();
        query.eq("announcement_id",announcementId);
        communityAnnouncementService.remove(query);
        if(i){
            return R.ok("删除公告成功！");
        }
        return R.error("删除公告失败！");

    }

    //编辑社区公告
    @PostMapping("editAnnouncement")
    public R editAnnouncement(@RequestBody ForumAnnouncement forumAnnouncement){
        boolean i = announcementService.updateById(forumAnnouncement);
        if(i==true){
            return R.ok("编辑公告成功！");
        }
        return R.error("编辑公告失败！");
    }


}

